package org.example.backend.Module.School.Course.Controller;

import cn.hutool.core.bean.BeanUtil;
import org.example.backend.Base.Response.JsonResult;
import org.example.backend.Module.School.Course.Pojo.DTO.CourseDTO;
import org.example.backend.Module.School.Course.Pojo.Entity.Course;
import org.example.backend.Module.School.Course.Service.CourseService;
import org.example.backend.Module.System.User.Pojo.Entity.MyUserDetails;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/v1/school/course/type")
public class CourseController {

    @Resource
    private CourseService courseService;

    @GetMapping("list")
    public JsonResult list(@RequestParam(required = false) String name, @RequestParam(required = false) Boolean enabled) {
        return JsonResult.ok(courseService.list(name, enabled));
    }

    @PostMapping("add")
    public JsonResult add(@RequestBody @Validated(value = CourseDTO.OnAdd.class) CourseDTO course, @AuthenticationPrincipal MyUserDetails userDetails) {
        courseService.addOrUpdate(BeanUtil.copyProperties(course, Course.class), userDetails.getUser());

        return JsonResult.ok();
    }

    @PostMapping("update")
    public JsonResult update(@RequestBody @Validated(value = CourseDTO.OnUpdate.class) CourseDTO course) {
        courseService.addOrUpdate(BeanUtil.copyProperties(course, Course.class), null);

        return JsonResult.ok();
    }

    @GetMapping("delete/{id}")
    public JsonResult delete(@PathVariable Long id) {
        courseService.delete(id);

        return JsonResult.ok();
    }
}
